package xhtml2pdf;

import java.awt.Color;
import java.util.HashMap;

import org.xml.sax.Attributes;

import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;

public class StyleParser {
	public static int[] getRGB(String rgbStr){
		char[] rgbChar = rgbStr.toCharArray();
		int[] rgb = new int[3];
		int offset = 0;
		int rgbArrayNum = 0;
		int i = 0;		
		rgbStr = new String(rgbChar);
		while(rgbChar[i] != '(') i++;
		offset = i+1;
		while( i < rgbChar.length){
			while(rgbChar[i] != ',' && rgbChar[i] != ')' && i < rgbChar.length-1){
				i++;
			}
			if(rgbArrayNum < 3){
				rgb[rgbArrayNum++] = Integer.parseInt((rgbStr.substring(offset,i)));
			}
			offset = ++i;
		}
		return rgb;	
	}
	public static HashMap getStyle(String str){
		HashMap<String,String> styles = new HashMap<String,String>();
		char[] styleChar = eliminateBlank(str).toCharArray();
		int offset = 0;
		String key;
		String value;
		str = new String(styleChar);
		for(int i = 0 ; i < styleChar.length ; i++){
			offset = i;
			while(i < styleChar.length-1 && styleChar[i] != ':')i++;
			key = str.substring(offset, i);
			offset = ++i;
			while(i < styleChar.length-1 && styleChar[i] != ';')i++;
			value = str.substring(offset, i);
			styles.put(key, value);
		}		
		return styles;
	}
	public static Element getStyledElement(Element e, Attributes attrs){
		if(e instanceof Paragraph){
			Paragraph p = (Paragraph)e;
			if(attrs.getIndex(XHtmlTags.STYLE) != -1){
				HashMap hm = StyleParser.getStyle(attrs.getValue(XHtmlTags.STYLE));
				if(hm.containsKey("text-align")){
					String align = (String)hm.get("text-align");								
					if(align.equals("justify")){
						p.setAlignment(Element.ALIGN_LEFT);
					}else if(align.equals("center")){
						p.setAlignment(Element.ALIGN_CENTER);
					}else if(align.equals("right")){
						p.setAlignment(Element.ALIGN_RIGHT);
					}
				}
				if(hm.containsKey("margin-left")){
					String marginLeft = (String)hm.get("margin-left");
					int marginNum = Integer.parseInt(marginLeft.substring(0,1));
					p.setIndentationLeft(marginNum*5);								
				}
			}			
			return p;
		}else if(e instanceof HumbrollChunk){
			HumbrollChunk chunk = (HumbrollChunk)e;
			if(attrs.getIndex("class") != -1){
				String attrValue = attrs.getValue("class");
				if(attrValue.equals("italic")) chunk.setStyle(Font.ITALIC);
				if(attrValue.equals("underline")) chunk.setStyle(Font.UNDERLINE);
				if(attrValue.equals("strike")) chunk.setStyle(Font.STRIKETHRU);
				if(attrValue.equals("highlight")){
					if(attrs.getIndex(XHtmlTags.STYLE) != -1){
						HashMap map = StyleParser.getStyle((String)attrs.getValue(XHtmlTags.STYLE));
						int rgb[] = new int[3];
						if(map.containsKey("background-color")){
							rgb = StyleParser.getRGB((String)map.get("background-color")); 
						}else{
							rgb[0] = rgb[1] = rgb[2] = 0;
						}
						chunk.setBackground(new Color(rgb[0],rgb[1],rgb[2]));
					}
				}
			}else if(attrs.getIndex(XHtmlTags.STYLE)!= -1){
				HashMap map = StyleParser.getStyle((String)attrs.getValue(XHtmlTags.STYLE));
				int rgb[] = new int[3];
				if(map.containsKey("color")){
					rgb = StyleParser.getRGB((String)map.get("color")); 
				}else{
					rgb[0] = rgb[1] = rgb[2] = 0;
				}					 
				chunk.setColor(new Color(rgb[0],rgb[1],rgb[2]));
			}
			return chunk;
		}else if(e instanceof List){
			List list = (List)e;
			if(attrs.getIndex(XHtmlTags.STYLE) != -1){
				HashMap map = StyleParser.getStyle((String)attrs.getValue(XHtmlTags.STYLE));
				if(map.containsKey("margin-left")){
					String marginLeft = (String)map.get("margin-left");
					list.setIndentationLeft(10*Integer.parseInt(marginLeft.substring(0,1)));
				}
			}
			
			return list;
		}
		return null;
	}  
	public static String eliminateBlank(String str){
		str = str.replaceAll("\\s*","");
		return str;
	}
	
}
